<?php
// application/models/GuestbookMapper.php

class Application_Model_SimCMSUserMapper
{
        protected $_dbTable;
     
        public function setDbTable($dbTable)
        {
            if (is_string($dbTable)) {
                $dbTable = new $dbTable();
            }
            if (!$dbTable instanceof Zend_Db_Table_Abstract) {
                throw new Exception('Ungültiges Table Data Gateway angegeben');
            }
            $this->_dbTable = $dbTable;
            return $this;
        }
     
        public function getDbTable()
        {
            if (null === $this->_dbTable) {
                $this->setDbTable('Application_Model_DbTable_SimCMSUser');
            }
            return $this->_dbTable;
        }
     
        public function save(Application_Model_SimCMSUser $user)
        {
            $data = array(
                'id'   => $user->getId(),
                'firstname' => $user->getFirstname(),
                'name' => $user->getName(),
                'nickname' => $user->getNickname(),
                'email' => $user->getEmail(),
                'password' => $user->getPassword(),
                'rank' => $user->getRank(),
                'lastlogin' => $user->getLastlogin(),
                'created' => $user->getCreated()
            );
     
            if (null === ($id = $user->getId())) {
                unset($data['id']);
                $this->getDbTable()->insert($data);
            } else {
                $this->getDbTable()->update($data, array('id = ?' => $id));
            }
        }
     
        public function find($id, Application_Model_SimCMSUser $user)
        {
            $result = $this->getDbTable()->find($id);
            if (0 == count($result)) {
                return;
            }
            $row = $result->current();
            $user->setId($row->id)
                      ->setFirstname($row->firstname)
                      ->setName($row->name)
                      ->setNickname($row->nickname)
                      ->setEmail($row->email)
                      ->setPassword($row->password)
                      ->setRank($row->rank)
                      ->setLastlogin($row->lastlogin)
                      ->setCreated($row->created);
        }
     
        public function fetchAll()
        {
            $resultSet = $this->getDbTable()->fetchAll();
            $entries   = array();
            foreach ($resultSet as $rows) {
                $entry = new Application_Model_SimCMSUser();
                $entry->setId($rows->id);
                $entry->setFirstname($rows->firstname);
                $entry->setName($rows->name);
                $entry->setNickname($rows->nickname);
                $entry->setEmail($rows->email);
                $entry->setPassword($rows->password);
                $entry->setRank($rows->rank);
                $entry->setLastlogin($rows->lastlogin);
                $entry->setCreated($rows->created);
                $entries[] = $entry;
            }
            return $entries;
        }

}

